package com.star.service;

import com.star.repository.PictureRepository;
import com.star.pojo.Picture;
import com.star.pojo.dto.PictureReq;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;

/**
 * 照片墙服务
 *
 * @author tanyapeng
 * @date 2021/2/5
 */
@Service
public class PictureService {

    @Resource
    private PictureRepository pictureRepository;

    /**
     * 照片墙列表
     *
     * @return
     */
    public List<Picture> listPicture() {
        return pictureRepository.listPicture();
    }

    /**
     * 新增照片墙
     *
     * @param res
     * @return
     * @throws IOException
     */
    public int savePicture(PictureReq res) throws IOException {
        res.getPictureaddress().transferTo(Paths.get("/home/nginx/kzimg/" + res.getPictureaddress().getOriginalFilename()));
        String pictureName = "/kzimg/" + res.getPictureaddress().getOriginalFilename();
        Picture picture = new Picture();
        BeanUtils.copyProperties(res, picture);
        picture.setPictureaddress(pictureName);
        return pictureRepository.savePicture(picture);
    }

    /**
     * 获取照片信息
     *
     * @param id
     * @return
     */
    public Picture getPicture(Long id) {
        return pictureRepository.getPicture(id);
    }

    /**
     * 更新照片墙
     *
     * @param picture
     * @return
     */
    public int updatePicture(Picture picture) {
        return pictureRepository.updatePicture(picture);
    }

    /**
     * 删除照片墙
     *
     * @param id
     */
    public void deletePicture(Long id) {
        pictureRepository.deletePicture(id);
    }

}